#!/bin/bash
###############################################################################
#       脚本名称: test_env.sh
#       功能描述: ENV处理器(env.sh)单元测试
###############################################################################

cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1

source ../test_framework.sh
source ../../../scripts/config/env.sh

TEST_DIR=$(create_test_dir)

###############################################################################
# 测试用例
###############################################################################

test_env_validate() {
    local test_file="$TEST_DIR/valid.env"
    cat > "$test_file" << 'EOF'
APP_NAME=MyApp
APP_VERSION=1.0.0
export DB_HOST=localhost
EOF
    
    env_validate "$test_file" 2>/dev/null
}

test_env_read() {
    local test_file="$TEST_DIR/read_test.env"
    cat > "$test_file" << 'EOF'
APP_NAME=MyApp
DB_HOST=localhost
DB_PORT=3306
EOF
    
    local name=$(env_read "$test_file" "APP_NAME" 2>/dev/null)
    assert_equals "MyApp" "$name"
    
    local port=$(env_read "$test_file" "DB_PORT" 2>/dev/null)
    assert_equals "3306" "$port"
}

test_env_write() {
    local test_file="$TEST_DIR/write_test.env"
    
    env_write "$test_file" "APP_NAME" "TestApp" 2>/dev/null
    env_write "$test_file" "DB_PORT" "3306" 2>/dev/null
    
    local name=$(env_read "$test_file" "APP_NAME" 2>/dev/null)
    assert_equals "TestApp" "$name"
}

test_env_delete() {
    local test_file="$TEST_DIR/delete_test.env"
    cat > "$test_file" << 'EOF'
APP_NAME=MyApp
TEMP_VAR=should_be_deleted
DB_HOST=localhost
EOF
    
    env_delete "$test_file" "TEMP_VAR" 2>/dev/null
    
    local temp=$(env_read "$test_file" "TEMP_VAR" 2>/dev/null)
    assert_empty "$temp"
    
    local name=$(env_read "$test_file" "APP_NAME" 2>/dev/null)
    assert_equals "MyApp" "$name"
}

test_env_list_keys() {
    local test_file="$TEST_DIR/list_keys_test.env"
    cat > "$test_file" << 'EOF'
APP_NAME=MyApp
APP_VERSION=1.0.0
DB_HOST=localhost
EOF
    
    local keys=$(env_list_keys "$test_file" 2>/dev/null)
    
    assert_contains "$keys" "APP_NAME"
    assert_contains "$keys" "APP_VERSION"
    assert_contains "$keys" "DB_HOST"
}

test_env_merge() {
    local target="$TEST_DIR/target.env"
    local source="$TEST_DIR/source.env"
    
    echo "APP_NAME=TargetApp" > "$target"
    cat > "$source" << 'EOF'
APP_NAME=SourceApp
DB_PORT=3306
EOF
    
    env_merge "$target" "$source" "merge" 2>/dev/null
    
    local name=$(env_read "$target" "APP_NAME" 2>/dev/null)
    assert_equals "TargetApp" "$name" "merge模式应该保留target的值"
    
    local port=$(env_read "$target" "DB_PORT" 2>/dev/null)
    assert_equals "3306" "$port"
}

test_env_format() {
    local test_file="$TEST_DIR/format_test.env"
    cat > "$test_file" << 'EOF'
APP_NAME=MyApp   


DB_HOST=localhost
EOF
    
    env_format "$test_file" 2>/dev/null
    
    assert_file_exists "$test_file"
    env_validate "$test_file" 2>/dev/null
}

test_env_hash() {
    local test_file="$TEST_DIR/hash_test.env"
    echo "APP_NAME=MyApp" > "$test_file"
    
    local hash1=$(env_get_hash "$test_file" 2>/dev/null)
    assert_not_empty "$hash1"
    
    env_write "$test_file" "APP_NAME" "ModifiedApp" 2>/dev/null
    
    local hash2=$(env_get_hash "$test_file" 2>/dev/null)
    assert_not_equals "$hash1" "$hash2"
}

###############################################################################
# 运行测试
###############################################################################

test_suite_start "ENV处理器 (env.sh) 单元测试"

run_test "ENV文件验证" test_env_validate
run_test "读取ENV值" test_env_read
run_test "写入ENV值" test_env_write
run_test "删除ENV键" test_env_delete
run_test "列出所有键" test_env_list_keys
run_test "合并ENV文件" test_env_merge
run_test "格式化ENV文件" test_env_format
run_test "哈希计算" test_env_hash

test_suite_end

cleanup_test_dir "$TEST_DIR"

test_report
exit $?

